import React from 'react'
import {Switch,Route,Redirect} from 'react-router-dom'
import {RouterInterface} from './router.d'

interface Iprops{
    routes:RouterInterface[]
}

function RouterView({routes}:Iprops){
    let routeArr = routes.filter((item:RouterInterface) => item.component);
    let RedirectArr = routes.filter((item:RouterInterface) => item.redirect);
    return <Switch>
        {routeArr.map((item:RouterInterface) => <Route  key={item.path} path={item.path} render={(props) => {
            return <item.component {...props} routes={item.children}></item.component>
        }}></Route>)}
        {RedirectArr.map((item:RouterInterface) => <Redirect key={item.path} from={item.path} to={item.redirect}></Redirect>)}
    </Switch>
}

export default RouterView